﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    //
    public class JLTAB50DOCData : BaseFunction
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        SETINTEXDB INTEXDB = new SETINTEXDB();
        public JLTAB50DOCData()
            : base()
        {

        }
        public SETINTEXDB BuildData(string client, string KRS, string LFD, string _KDAUFTNR)
        {
            DataSet SResult = new DataSet();
            SETINTEXDB SReturn = new SETINTEXDB();

            try
            { 
                DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();
                if (_KDAUFTNR.Trim().Length > 1)
                {
                    dwData["ABKKDAUFTNR"] = _KDAUFTNR;
                    DataSet STemp = INTEXDBC.QueryABKOPF(dwData, 6);
                    foreach (DataRow row in STemp.Tables[0].Rows)
                    {
                        KRS = row["ABKAUFKRS"].ToString();
                        LFD = row["ABKAUFLFD"].ToString();
                        SResult = INTEXBL.QueryABK(MANDANT, KRS, LFD);
                        SReturn.Merge(SResult);
                    }
                }
                else
                {
                    SResult = INTEXBL.QueryABK(MANDANT, KRS, LFD); 
                    SReturn.Merge(SResult);
                }                

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                if (SReturn.Tables.Contains("ABPOS"))
                {
                    foreach (DataRow row in SReturn.Tables["ABPOS"].Rows)
                    {
                        row["ABPART"] = Helper.Convert8859P1ToGB2312(row["ABPART"].ToString());
                    }
                }
            }
            return SReturn;
        }

    }
}